package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tongchen
 * @create 2023-02-06 10:55
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    DishService dishService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(ShoppingCart shoppingCart,HttpSession session) {

        log.info(shoppingCart.toString());
        //查询用户id
        Long userId = (Long) session.getAttribute("user");
        //根据用户id查询数据库的数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( ShoppingCart::getUserId, userId);
        //对查出的数据进行排序
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);

    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        //设置当前用户id
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        //获取当前dishid进行分类
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
            queryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询信息
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //保存更新或修改信息
        if (cartServiceOne != null) {
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            shoppingCartService.updateById(cartServiceOne);
        }
        //创建新的信息
        else {
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 清空购物车的操作
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> removeShoppingCart(HttpSession httpSession){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //获取用户id
        Long user = (Long) httpSession.getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId,user);
        shoppingCartService.remove(queryWrapper);
        return R.success("删除购物车成功");
    }
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart,HttpSession httpSession){
        log.info(shoppingCart.toString());
        //获取用户信息
        Long userId = (Long) httpSession.getAttribute("user");
        //获取setmeal还是dish
        LambdaQueryWrapper<ShoppingCart> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId==null){
            updateWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }else {
            updateWrapper.eq(ShoppingCart::getDishId, dishId);
        }
        //查询信息
        ShoppingCart one = shoppingCartService.getOne(updateWrapper);
        //更新信息
        one.setNumber(one.getNumber()-1);
        shoppingCartService.updateById(one);
        return R.success(one);
    }
}
